// 左侧按钮
$(function(){
    $.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name ]){
                if (!o[this.name ].push) {
                    o[this.name ] = [o[this.name ]];
                }
                o[this.name ].push(this.value || '');
            }else {
                o[this.name ] = this.value || '';
            }
        });
        return o;
    };
    var isTure=false; 
    $(".disasterTypeSelect").click(function(){
        $(this).addClass("border");
        $(".disasterProcessSelect").removeClass("border");
        $(".disasterFirstPart").removeClass("displayNone");
        $(".disasterSecondPart").addClass("displayNone");
    })
    $(".disasterProcessSelect").click(function(){
        $(this).addClass("border");
        $(".disasterTypeSelect").removeClass("border");
        $(".disasterFirstPart").addClass("displayNone");
        $(".disasterSecondPart").removeClass("displayNone");
    })

    // 添加按钮
    $(".add").click(function(){
        if($(this).val()==='添加'){
            $(this).val('收起').parent('h2').next('ul').slideDown();
        }else{
            $(this).val('添加').parent('h2').next('ul').slideUp();
        }
    });


    // 表单验证
    function isInt(dom){
        if(!regInt.test(dom.val())){
            dom.next().html("请输入整数");
            dom.addClass("invalid");
            dom.parents("li").attr("class","false");
            return false;
        }else{
            dom.next().html("");
            dom.removeClass("invalid");
            dom.parents("li").attr("class","true");
        }
    }
    //最大字符数
    function maxLength(dom,str){
        if(str.length>255){
            dom.next().html("最大字符255");
            dom.addClass("invalid");
            dom.parents("li").attr("class","false");
            return false;
        }else{
            dom.next().html("");
            dom.removeClass("invalid");
            dom.parents("li").attr("class","true");
        }
    }
    //非空
    function isEmpty(dom,str){
        if(str==""){
            dom.next().html("内容不能为空");
            dom.addClass("invalid");
            dom.parents("li").attr("class","false");
            return false;
        }else{
            dom.next().html("");
            dom.removeClass("invalid");
            dom.parents("li").attr("class","true");
        }
    }

    var form=document.forms[0];
    var txtId=form.id;
    var txtName=form.name;






    var regInt=/^[\d]{1,}$/;

    $(".int").blur(function(){
        isInt($(this));
    });
    $(".maxLength").blur(function(){
        var str=$(this).val();
        maxLength($(this),str)
    });
    $(".empty").blur(function(){
        var str=$(this).val();
        isEmpty($(this),str)
    });


    //验证提交
    $("form").submit(function(e){
        var liLength=$(this).find(".disaster>li").length;
        console.log(liLength);
        for(var i=0;i<liLength-1;i++){
            if($(this).find(".disaster>li").eq(i).attr("class")!=="true"){
                e.preventDefault();
                console.log("提交无效")
                return;
            }
        }
        
        console.log($("select").val());
    })

    $(".addSubtype ").click(function(){
        $(".addSubtypeContent").removeClass("displayNone").addClass("displayShow");
    })
    $(".addSubStrenth").click(function(){
        $(".addSubStrenthContent").removeClass("displayNone").addClass("displayShow");
    })
    //灾害类型添加数据库
    $("#addDisaster input[type=button]").click(function(){
       var that= $(this)
        $.ajax({
            url:"http://localhost:8080/type/addType",
            type:"post",
            dataType:"json",
            data:JSON.stringify($("#addDisaster").serializeObject()),
            contentType:"application/json;charset=utf-8",
            success:function(data){
                var id=data
                if(!isNaN(data)){
                    console.log(that.siblings("button"))
                    that.siblings("button").prop("disabled",false)
                    $("#disasterSubtype .sub").append('<input type="hidden" name="disasterTypeId" value="'+id+'">').addClass("displayShow").removeClass("displayNone")
                }
                    
                console.log(data)
            }
        })
    })
    //增加子类显示
    $(".newSubtype").click(function(){
        $("#disasterSubtype>div").addClass("displayShow").removeClass("displayNone")
        $(this).parent().addClass("displayNone").removeClass("displayShow")
        $("#disasterSubtype>input[type='text']").val(" ") 
    })
    //增加子类型强度显示
    $(".newSubtypeStrength").click(function(){
        $("#disasterStrength>div").addClass("displayShow").removeClass("displayNone")
        $(this).parent().addClass("displayNone").removeClass("displayShow") 
    })
    //灾害子类型添加数据库
   $("#disasterSubtype input[type=button]").click(function(){
       var that= $(this)
        $.ajax({
            url:"http://localhost:8080/type/addSubtype",
            type:"post",
            dataType:"json",
            data:JSON.stringify($("#disasterSubtype").serializeObject()),
            contentType:"application/json;charset=utf-8",
            success:function(data){
                var id=data
                if(!isNaN(data)){
                    console.log(that.siblings("button"))
                    //追加一个隐藏的input元素，存放disasterSubTypeId
                    $("#disasterStrength").append('<input type="hidden" name="disasterSubtypeId" value="'+id+'">')
                    //显示子灾害名称
                    var nameVal=that.parent().siblings("ul").find('input[name="name"]').val();
                    $(".showSubtype").prepend('<span>'+nameVal+'</span>')
                    $("#disasterStrength .sub").addClass("displayShow").removeClass("displayNone")
                }
                console.log(data,nameVal)
            }
        })
        console.log(JSON.stringify($("#disasterSubtype").serializeObject()))
    })  
   //灾害子类型强度添加数据库
    $("#disasterStrength input[type=button]").click(function(){
       var that= $(this)
        $.ajax({
            url:"http://localhost:8080/type/addStrength",
            type:"post",
            dataType:"json",
            data:JSON.stringify($("#disasterStrength ").serializeObject()),
            contentType:"application/json;charset=utf-8",
            success:function(data){
                console.log(data)
                var id=data
                //if(!isNaN(data)){
                    //console.log(that.siblings("button"))
                    //追加一个隐藏的input元素，存放disasterSubTypeId
                    // $("#disasterSubtype").append('<input type="hidden" name="disasterSubTypeId" value="'+id+'">')
                    //显示子灾害名称
                   
                    
                //}
                console.log(nameVal)
            },
            error:function(error){
                    console.log(error)
            }

        }) 
        var nameVal=that.parent().siblings("ul").find('input[name="level"]').val();
                    $(".showSubtypeStrength").prepend('<span>'+nameVal+'</span>')
        console.log(JSON.stringify($("#disasterStrength").serializeObject()))
    })         
    //关闭按钮
    $(".close").click(function(){
        $(this).parent().addClass("displayNone").removeClass("displayShow")
        $(this).parent().prev().addClass("displayShow").removeClass("displayNone")
    })
     
})
